from AIoT_paddle_tools import *
from flask import Flask, request
import cv2
import oss2
import time
import numpy as np
import requests
import json

app = Flask(__name__)


# start 申请
# 申请一个人车猪检测的client和预处理序列
seq_pig, client_pig = paddle_client_create('person_pig_car')
# 申请一个工作服检测的client和预处理序列
seq_cls, client_cls = paddle_client_create('suit')
# end 申请


def paddle(url):
    # 返回一个json：[{'type': 1, 'rect': [0.2038875379060, 0.3378119468688, 0.08795095744885, 0.38912479501021]},[],[],[]...]
    result, img = paddle_model_person_pig_car(url, 0, seq_pig, client_pig, 0.5, False)

    print(result)
    # 判断是是否返回人 继续执行识别工作服
    for item in result:
        if (item['type'] == 1):
            # 图片裁剪
            image = paddle_get_suit(item, img)
            # 请求服务器工作服
            suit = paddle_model_suit(image, 2, seq_cls, client_cls, 0.5)
            # 拿到结果拼接字符
            suit_dict = {'suit': suit}
            item.update(suit_dict)

    return item


@app.route('/start', methods=['POST'])
def count_init():
    # id = request.args.get('ID')
    # rtsp = request.args.get('RTSP')
    # setting = request.args.get('SETTING', '')
    # print(id)
    # print(rtsp)
    # print(setting)
    url = 'https://i.ys7.com/streamer/alarm/url/get?fileId=20201103101403-E72197187-1-10014-2-1&deviceSerialNo=E72197187&cn=1&isEncrypted=0&isCloudStored=0&ct=4&lc=7&bn=4_hikalarm&isDevVideo=0'
    item = paddle(url)
    return item


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8012)
